import CryptoJS from 'crypto-js'
import store from '@/store'
import constants from '@/module/constants'
export const urlPathHandle = (obj = {}) => {
  let str = ''
  for (const key in obj) {
    str += `${key}=${obj[key]}&`
  }
  return str.slice(0, str.length - 1)
}
export const urlToObjHandle = (str = '') => {
  // 传入的格式 sbd_gzp@userNo=P31173515&ticketType=subOne&mRID=f62f812f-e600-413a-9afe-980958258a8d
  const strArr = str.split(/@|&/g)
  const obj = {}
  strArr.forEach(item => {
    if (item) {
      const itemArr = item.split('=')
      if (itemArr.length > 1) {
        obj[itemArr[0]] = itemArr[1]
      } else {
        obj.firm = itemArr[0]
      }
    }
  })
  // 返回的是一个对象
  return obj
}
export const ticketPushType = () => {
  const ticketType = constants.ticketType
  const type = store.state.ticketType
  let ticketInterType = ''
  switch (type) {
    case ticketType.LINE_ONE:
      ticketInterType = 'SGLineFirstWorkTicket'
      break
    case ticketType.LINE_TWO:
      ticketInterType = 'SGLineSecondWorkTicket'
      break
  }
  return ticketInterType
}
// 加密
export const wordEncrypt = (word, keyVal = 'ECB024F249153D401D1A0D691D62B77F') => {
  const key = CryptoJS.enc.Utf8.parse(keyVal)
  const srcs = CryptoJS.enc.Utf8.parse(word)
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypted.toString()
}
// 解密
export const wordDecrypt = (word, keyVal = 'ECB024F249153D401D1A0D691D62B77F') => {
  const key = CryptoJS.enc.Utf8.parse(keyVal)
  const decrypted = CryptoJS.AES.decrypt(word, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return decrypted.toString(CryptoJS.enc.Utf8)
}
